package com.acceptable.qucun.generic.filter;

import com.acceptable.qucun.generic.util.StringFormatUtil;
import com.acceptable.qucun.generic.util.verify.FormatVerifyUtil;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class FormatVerifyFilter extends GenericFilterBean {

    /**
     *  对前端传来的main_开头的数据更换key 并进行格式验证
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
//         System.out.println("com.acceptable.web.qucun.other.filter.FormatVerifyFilter.doFilter");
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        Map<String, String> dataMap = (Map<String, String>) request.getAttribute("data_map");
        if(dataMap == null || dataMap.size() == 0){
            filterChain.doFilter(request, response);
            return;
        }

        Iterator<Map.Entry<String, String>> entryIterator = dataMap.entrySet().iterator();
        List<String> oldKeyList = new ArrayList<>();

        while (entryIterator.hasNext()){
            Map.Entry<String, String> entry = entryIterator.next();
            String originKey = entry.getKey();
            String value = entry.getValue();
            if(!originKey.contains("main_")){
                continue;
            }

            oldKeyList.add(originKey);
            String newKey = originKey.replace("main_", "");
            String methodName = "is" + StringFormatUtil.capitalToUpperCase(StringFormatUtil.underlineToLowerCamel(newKey));

            try {
                Method method = FormatVerifyUtil.class.getDeclaredMethod(methodName, String.class);
                boolean isValid = (boolean) method.invoke(new FormatVerifyUtil(), value);
//                 System.out.println(methodName + " " + isValid + " " + value);
                if (!isValid) {
                    response.setCharacterEncoding("UTF-8");
                    response.getWriter().write("格式验证出错, 请检查数据格式。");
                    return;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        for (String oldKey: oldKeyList){
            String newKey = oldKey.replace("main_", "");
            dataMap.put(newKey, dataMap.get(oldKey));
            dataMap.remove(oldKey);
        }

        request.setAttribute("data_map", dataMap);
        filterChain.doFilter(request, response);
    }
}
